package main

import (
	//"encoding/json"
	"GameAppServer/datas"
	"fmt"

	//	"github.com/playnb/mustang/global"
	//	"github.com/playnb/mustang/log"
	"github.com/playnb/mustang/nosql"
	//	"github.com/playnb/mustang/sqldb"
	//"github.com/playnb/mustang/utils"
	"math/rand"
	"net/http"

	//	"github.com/playnb/mustang/weixin"
	//"net/url"
	"strconv"
	"strings"
	//"time"

	_ "net/http/pprof"
)

func handleRoot(w http.ResponseWriter, r *http.Request) {
	html := `
<HTML>
<TITLE>摘星互动</TITLE>
<HEAD>
	<meta name="baidu_union_verify" content="c003a113c4f2657c5eb55a53a264b298">
	<meta name='EEEQI_verify' content='5f29c139a8c201c9b978e633c9227aed'>
	<meta name="ads8_union_verify" content="58ab707e2e1645965ac7883ad71652cc">
	<meta property="qc:admins" content="627042637760411626654" />
	<meta name="domain_verify" content="pmrgi33nmfuw4ir2ejygyylznzrc43tfoqrcyithovuwiir2ei3gezddgzqtknjsmuywgnbtgq4dqnjtha4wgndghaytombqhezwkirmej2gs3lfknqxmzjchiytinjtgy2diojqgy4demt5">
</HEAD>
<meta http-equiv="Refresh" content="2;url=http://www.playnb.net/apps/jump/"> 
<BODY>
	
</BODY>
</HTML>
`
	fmt.Fprintf(w, "%s", html)
}

func handleAds(w http.ResponseWriter, r *http.Request) {
	html := `
<HTML>
<TITLE>摘星互动</TITLE>
<BODY>
hello
<div>
	<script type="text/javascript">
var _madParams = {
  transferTime:10000,
  app:"f3ca3667622bf43c", pos:"90050519",
  callback:function(){/* alert("this is a callback"); */},
  debug:1
};
</script><script type="text/javascript" src="http://appsrv1.madserving.cn/jsa/ss_showad.js"></script>
</div>
world
</BODY>
</HTML>
`
	fmt.Fprintf(w, "%s", html)
}

func resetByOpenID(openID string) {
	for _, key := range nosql.Redis.Keys("*" + openID).Val() {
		nosql.Redis.Del(key)
	}
}

func resetBestScore(w http.ResponseWriter, r *http.Request) {
	for i := 0; i < 200; i++ {
		nosql.Redis.Del("ST:BEST_SCORE_" + strconv.Itoa(i))
	}
	fmt.Fprintf(w, "最高分清空")
}

func resetShareFirends(w http.ResponseWriter, r *http.Request) {
	nosql.Redis.Del("ST:ShareFirends")
	fmt.Fprintf(w, "好友分享清0")
}
func resetShareTimeline(w http.ResponseWriter, r *http.Request) {
	nosql.Redis.Del("ST:ShareTimeline")
	fmt.Fprintf(w, "朋友圈分享清0")
}

func handleDefaultData(w http.ResponseWriter, r *http.Request) {
	resetByOpenID("oUDwYuOlgnjx9z2pEQlTZ85Elsxo")
	resetByOpenID("oqxYYs5Y8h0_UTpKJLngn6VLNb_8")
	resetByOpenID("oUDwYuB3HdzQbpeUbVmXtk8rIFGU")

	err := nosql.Redis.SAdd("NewFriend:oUDwYuOlgnjx9z2pEQlTZ85Elsxo", "oqxYYs5Y8h0_UTpKJLngn6VLNb_8").Err()
	if err != nil {
		fmt.Println(err)
	}
	nosql.Redis.SAdd("NewFriend:oqxYYs5Y8h0_UTpKJLngn6VLNb_8", "oUDwYuOlgnjx9z2pEQlTZ85Elsxo")
	nosql.Redis.SAdd("NewFriend:oUDwYuB3HdzQbpeUbVmXtk8rIFGU", "oqxYYs5Y8h0_UTpKJLngn6VLNb_8")
	nosql.Redis.SAdd("NewFriend:oqxYYs5Y8h0_UTpKJLngn6VLNb_8", "oUDwYuB3HdzQbpeUbVmXtk8rIFGU")
	//	nosql.Redis.SAdd("NewFriend:oUDwYuB3HdzQbpeUbVmXtk8rIFGU", "oUDwYuOlgnjx9z2pEQlTZ85Elsxo")
	//	nosql.Redis.SAdd("NewFriend:oUDwYuOlgnjx9z2pEQlTZ85Elsxo", "oUDwYuB3HdzQbpeUbVmXtk8rIFGU")

	nosql.Redis.Set("GameUser:oUDwYuB3HdzQbpeUbVmXtk8rIFGU", `{"openID":"oUDwYuB3HdzQbpeUbVmXtk8rIFGU","userName":"BBB","bestScore":0,"diamonds":0,"character":"tuzi","iconURL":"http://wx.qlogo.cn/mmopen/eHEMOF2hOenZOGp7QSKpTCMnhcTRNgVNPiaQyenI1x3MAjkhpEicb2zFnBfxFXSmTLs6K2TicqrdsTEXavrKutvHojlDckEzicUC/0","RealBestScore":0}`, 0)
	nosql.Redis.Set("GameUser:oUDwYuOlgnjx9z2pEQlTZ85Elsxo", `{"openID":"oUDwYuOlgnjx9z2pEQlTZ85Elsxo","userName":"CCC","bestScore":0,"diamonds":0,"character":"tuzi","iconURL":"http://wx.qlogo.cn/mmopen/L7SLTbQwETnAqjqAbms5xahwdgRQ1Oy4Kwb0INlCs7ZHewrVAqiatCyxYMgiaSzJpTv9Ele3kTPViaYic7hJAFajiazQsvlHo1o2K/0","RealBestScore":0}`, 0)
	nosql.Redis.Set("GameUser:oqxYYs5Y8h0_UTpKJLngn6VLNb_8", `{"openID":"oqxYYs5Y8h0_UTpKJLngn6VLNb_8","userName":"AAA","bestScore":0,"diamonds":0,"character":"tuzi","iconURL":"http://wx.qlogo.cn/mmopen/qicbKu6iaOLzuvNXviaJBK2LdSGnje7BFS5Nq37N5aDAIjyoBNI1N07UNKXwzcMMQKPljwMpKicQUc5Hx5PVV3Z9aibxklGOmghfl/0","RealBestScore":18}`, 0)

	nosql.Redis.Set("Login:f359024b2850009bbf2fe5d713f4f4b61489c8d537d5eae49e9482cb0b468b9fc0f5ba8d3415055103829a31173a8bc8", "oqxYYs5Y8h0_UTpKJLngn6VLNb_8", 0)
	nosql.Redis.Set("Login:c14bfc43b3886fba7581254525ba5a45001c5a18950e91b838084dd529baa5b3632c81425e124e5050203204888a43b3", "oUDwYuB3HdzQbpeUbVmXtk8rIFGU", 0)
	nosql.Redis.Set("Login:33807e47a52d77740df33158ed2db10ab17595c4c7771aaa4ad0a60b58f5db9697276acd3cd1dc3e43b143b11de99cd6", "oUDwYuOlgnjx9z2pEQlTZ85Elsxo", 0)
	nosql.Redis.Set(`UserInfo:oqxYYs5Y8h0_UTpKJLngn6VLNb_8`, `{"openid":"oqxYYs5Y8h0_UTpKJLngn6VLNb_8","nickname":"AAA","sex":1,"language":"zh_CN","city":"松江","province":"上海","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/qicbKu6iaOLzuvNXviaJBK2LdSGnje7BFS5Nq37N5aDAIjyoBNI1N07UNKXwzcMMQKPljwMpKicQUc5Hx5PVV3Z9aibxklGOmghfl\/0","privilege":[],"unionid":"oiD34wm7PcKbMaiL0VZ1VI5N7vlY"}`, 0)
	nosql.Redis.Set(`UserInfo:oUDwYuB3HdzQbpeUbVmXtk8rIFGU`, `{"openid":"oUDwYuB3HdzQbpeUbVmXtk8rIFGU","nickname":"BBB","sex":0,"language":"zh_CN","city":"","province":"","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/eHEMOF2hOenZOGp7QSKpTCMnhcTRNgVNPiaQyenI1x3MAjkhpEicb2zFnBfxFXSmTLs6K2TicqrdsTEXavrKutvHojlDckEzicUC\/0","privilege":[],"unionid":"oiD34wtMqjyxkGzSSFR0JzZcXHkY"}`, 0)
	nosql.Redis.Set(`UserInfo:oUDwYuOlgnjx9z2pEQlTZ85Elsxo`, `{"openid":"oUDwYuOlgnjx9z2pEQlTZ85Elsxo","nickname":"CCC","sex":1,"language":"zh_CN","city":"松江","province":"上海","country":"中国","headimgurl":"http:\/\/wx.qlogo.cn\/mmopen\/L7SLTbQwETnAqjqAbms5xahwdgRQ1Oy4Kwb0INlCs7ZHewrVAqiatCyxYMgiaSzJpTv9Ele3kTPViaYic7hJAFajiazQsvlHo1o2K\/0","privilege":[],"unionid":"oiD34wm7PcKbMaiL0VZ1VI5N7vlY"}`, 0)

	fmt.Fprintf(w, "AAA,BBB,CCC出来了")
}

func handleResetByOpenID(w http.ResponseWriter, r *http.Request) {
	/*
		oUDwYuAGg5txPKx7oKhcEgY9MKQQ	蔡秀桐
		oUDwYuBc0oB5dvRSXvy4bM032u0U	浅水湾
		oUDwYuF0HhPNRzvGQp7av1t0wWMM	风清扬笑
		oUDwYuGiA4klEVrLPxquNScidWxE	小毛
		oUDwYuKgh_fOeS9ilV3UtUa_bgmU	jintemp
		oUDwYuKvmwSO3mdKnCcArQaOh5WQ	Lattimore Z.
		oUDwYuL1zCpZgsQYQZiDRGboUNDY	彩色铅笔
		oUDwYuOlgnjx9z2pEQlTZ85Elsxo	刘天鹏
	*/
	checkMap := make(map[string]string)

	checkMap["oUDwYuAGg5txPKx7oKhcEgY9MKQQ"] = "蔡秀桐"
	checkMap["oUDwYuBc0oB5dvRSXvy4bM032u0U"] = "浅水湾"
	checkMap["oUDwYuF0HhPNRzvGQp7av1t0wWMM"] = "风清扬笑"
	checkMap["oUDwYuGiA4klEVrLPxquNScidWxE"] = "小毛"
	checkMap["oUDwYuKgh_fOeS9ilV3UtUa_bgmU"] = "jintemp"
	checkMap["oUDwYuKvmwSO3mdKnCcArQaOh5WQ"] = "Lattimore Z."
	checkMap["oUDwYuL1zCpZgsQYQZiDRGboUNDY"] = "彩色铅笔"
	checkMap["oUDwYuOlgnjx9z2pEQlTZ85Elsxo"] = "刘天鹏"
	checkMap["oUDwYuB3HdzQbpeUbVmXtk8rIFGU"] = "摘星"
	r.ParseForm()
	openID := strings.Join(r.Form["OpenID"], "")
	if _, ok := checkMap[openID]; ok == true {
		user := datas.MakeUserData(openID)
		if user != nil {
			user.LoadFromDB()
			if user.HasInitBaseUserData {
				user.NeedSaveBaseData = true
				user.SaveBaseUserData()
				key := "PrivateScore:" + openID
				_, err := nosql.Redis.Del(key).Result()
				if err != nil {
					fmt.Fprintf(w, "%s ERROR %s", user.UserName, err.Error())
				} else {
					fmt.Fprintf(w, "%s 设置分数", user.UserName)
				}
				return
			}
		}
		fmt.Fprintf(w, "未知的OpenID : %s", openID)
	} else {
		fmt.Fprintf(w, "未授权的OpenID : %s", openID)
		return
	}
}

func handleResetAll(w http.ResponseWriter, r *http.Request) {

	nosql.Redis.FlushAll()

	html := `<html><body>哥!你把数据库清了....<p/><br/><a href="/DefaultData">点这里初始化账号</a><p/><br/>`
	switch rand.Int() % 5 {
	case 0:
		html = html + `昨天老师布置家庭作业太多了，就拿同桌小明的作业来抄。<p/>
抄着抄着，突然想到，作业是给自己写的，抄作业不能提高自己成绩啊！<p/>
想到这里，我啪啪给了自己两耳光：不能一心二用，赶紧抄作业！<p/>`
	case 1:
		html = html + `在路边电线杆上看到了两个广告：一个写的是“重金求子”，一个写的是“专治不孕不育”。<p/>
我想了想，好久没有做好事了，于是把“求子”的联系电话发给了“治孕”的人。<p/>`
	case 2:
		html = html + `在最近百年人类现代史中，发生大规模直接使用活人做试验的有三次：<p/>
1.德国法西斯大规模直接使用活人做毒气灭绝试验；<p/>
2.日本731部队大规模直接使用活人做细菌武器试验；<p/>
3.中国证监会大规模直接使用活人做熔断试验。<p/>`
	case 3:
		html = html + `妹子我一直以来熬夜，睡不着，听人说睡觉的时候在枕头边放个芒果可以睡的很好。<p/>
于是我凌晨一点钟起来把那个芒果吃了，太尼玛香了！<p/>`
	case 4:
		html = html + `“小学女生”听起来就很萝莉很萌，“小学男生”一听就脏兮兮傻乎乎的；<p/>
“初中女生”听起来就很清纯很可爱，“初中男生”一听就脏兮兮傻乎乎的；<p/>
“女高中生”听起来正是美到爆的时候，“男高中生”一听就脏兮兮傻乎乎的；<p/>
“女大学生”听起来就越发成熟越发有女人味，“大学男生”一听就脏兮兮傻乎乎的。<p/>`
	}
	html = html + "</html></body>"
	fmt.Fprintf(w, html)
	return
}
